//
//  CSDNTool.h
//
//
//  Created by 孟瑞瑞 on 16/5/17.
//  Copyright © 2016年 milan. All rights reserved.
//
//工具类。

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
#import "Constants.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import "CNLogManager.h"

/// 边框类型(位移枚举)
typedef NS_ENUM(NSInteger, UIBorderSideType) {
    UIBorderSideTypeAll    = 0,
    UIBorderSideTypeTop    = 1 << 0,
    UIBorderSideTypeBottom = 1 << 1,
    UIBorderSideTypeLeft   = 1 << 2,
    UIBorderSideTypeRight  = 1 << 3,
};

typedef void(^cleanCacheBlock)(void);

@interface CSDNTool : NSObject

//获取当前时间
+ (NSString *)nowTime;

//获取NetWork当前时间
+ (NSString *)netWorkNowTime;

// 验证验证码的长度
+ (BOOL)isValidCode:(NSString *)code;

// 验证密码的长度
+ (BOOL)isValidPassword:(NSString *)password;

// 验证用户名的长度
+ (BOOL)isValidUserName:(NSString *)userName;

//正则判断手机号码格式
+ (BOOL)isValidatePhone:(NSString *)phone;
//正则判断邮箱格式
+(BOOL)isValidateEmail:(NSString *)email;

+(BOOL)isValidUsernameStr:(NSString *)username;

//判断字典是否为空
+ (BOOL)isNullToDictionary:(NSDictionary *)dict;

#pragma mark --判断手机号合法性

/**
 判断手机号合法性
 
 @param phoneNumber 手机号码
 @param regex 正则
 @return 是否合法
 */
+ (BOOL)checkPhoneNumber:(NSString *)phoneNumber AndRegex:(NSString *)regex;

//将颜色转换成图片
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size;


#pragma mark -- 添加登录提示框
//添加登录提示框
+ (void)showLoginAlert:(NSString *)alertString inView:(UIView *)view;
//添加提示框
+(void)showAlert:(NSString *)alertString inView:(UIView *)view;
#pragma mark -- 添加提示框
//添加提示框
+ (void)showImageAlert:(NSString *)alertString inView:(UIView *)view;
+ (void)showErrorImageAlert:(NSString *)alertString inView:(UIView *)view;

//md5加密
/**
 *  md5加密
 *
 *  @param str 加密前的字符串
 *
 *  @return 返回加密后的字符串
 */
+(NSString *)md5:(NSString *)str;

#pragma mark -- base64加密
/**
 *  base64加密
 *
 *  @param str 加密前的字符串
 *
 *  @return 返回加密后的字符串
 */
+ (NSString *)base64Encode:(NSString *)string;

#pragma mark -- base64解密
/**
 *  base64解密
 *
 *  @param str 加密的字符串
 *
 *  @return 返回加密后的字符串
 */
+ (NSString *)base64Dencode:(NSString *)base64String;


#pragma mark - 时间转换
/**
 时间转换
 
 @param str 时间
 @return 时间
 */
+ (NSString *)compareCurrentTime:(NSString *)str;
+ (NSDate *)getTimeDate:(NSString *)str;

+ (NSString *)getTimeFromTimestamp:(double)time;

/**
 ** lineView:	   需要绘制成虚线的view
 ** lineLength:	 虚线的宽度
 ** lineSpacing:	虚线的间距
 ** lineColor:	  虚线的颜色
 **/
+ (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor;


//按要求截取字符串
+(NSString *)stringAtIndexByCount:(NSString *)string withCount:(NSInteger)count;

//编码
+(NSString *)URLEncodedString:(NSString *)str;
//解码
+(NSString *)URLDecodedString:(NSString *)str;

+(NSString *)replaceUnicode:(NSString *)unicodeStr;
#pragma mark//汉字转 Unicode   张三 →  \u5f20\u4e09
+(NSString *)utf8ToUnicode:(NSString *)string;

#pragma mark -- 清除路径下文件:
+(void)clearCache:(NSString *)path;

//清空 WKWebView cookie
+ (void)removeWebCookie;

//清空 WKWebView 缓存
+ (void)removeWebCache;

#pragma mark -- 计算整个路径下及其子文件夹下文件大小:
+(float)fileSizeAtPathInFilePath:(NSString *)filePath;

#pragma mark -- 根据字符串的的长度来计算UITextView的高度
/**
 
 @return 根据字符串的的长度来计算UITextView的高度
 */
+(float)heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width;

#pragma mark -- 字典转json格式字符串
//字典转json格式字符串：
+ (NSString*)dictionaryToJson:(NSDictionary *)dic;

#pragma mark -- json格式字符串转字典
//json格式字符串转字典：
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString;

#pragma mark 字典转json字符串，并直接过滤掉里面的非法字符
+ (NSString*)dictionaryToUnescapedCharacterJson:(NSDictionary *)dic;

#pragma mark - 去除json字符串中包含制表符
+(NSString *)removeUnescapedCharacter:(NSString *)inputStr;

#pragma mark - 去除json字符串中包含html标签
+ (NSString *)flattenHTML:(NSString *)html;

+(NSString *)stringByTrimmingWhitespaceAndNewlineStr:(NSString *)str;

+(NSAttributedString *)stringByTrimmingWhitespaceAndNewlineAttributedString:(NSAttributedString *)attributedStr;

//去掉double后面多余的0
+(NSString *)changeFloat:(NSString *)stringFloat;

//将url里面的参数转换成字典
+ (NSDictionary *)getParamsWithURL:(NSURL *)url;
+ (NSArray*)getParamsWithUrlString:(NSString*)urlString;
+(NSDictionary *)dictionaryWithUrlString:(NSString *)urlStr;
+ (NSString *)keyValueStringWithDict:(NSDictionary *)dict;

/**
 获取url的所有参数
 @param url 需要提取参数的url
 @return NSDictionary
 */
+(NSDictionary *)parameterWithURL:(NSURL *)url;

//获取iPhone 机型
+ (NSString *)getDeviceName;

/**
 *  设备版本
 *
 *  @return
 */
+ (NSString*)deviceVersion;

/**
 *  清理缓存
 */
+(void)cleanCache:(cleanCacheBlock)block;

// 获取Caches目录路径
+ (NSString *)getCachesPath;

/**
 *  整个缓存目录的大小
 */
+ (CGFloat)folderSizeAtPath:(NSString *)path;


/**
 *  判断是否是url
 */
+ (BOOL)isUrlAddress:(NSString*)url;

/**
 获取WebView截屏
 
 @param webView webView
 @return 图片
 */
+ (UIImage *)imageForWebView:(WKWebView *)webView;

//获取当前手机连接Wifi的名称 SSID
+ (NSString *)getWifiName;

//获取当前手机连接Wifi的mac地址
+ (NSString *)getWifiMacAddress;

//WIFI还是流量都能获取到IP地址
+ (NSString *)getIPAddress:(BOOL)preferIPv4;

// Get WIFI IP Address
- (NSString *)getWIFIIPAddress;

//获取UserAgent
+(NSString *)getUserAgent;

+(NSString *)getWebUserAgent;

+(NSString *)getMobileCountryCode;

/**
 随机数生成
 
 @param from
 @param to
 @return
 */
+(NSInteger)getRandomNumber:(NSInteger)from to:(NSInteger)to;

//获取当前时间 offsetTime 秒后的时间戳
+(NSString *)timeStampWithOffsetTime:(NSInteger)offsetTime;

//获取当前时间戳
+ (NSString *)currentTimeStampStr;

+ (NSTimeInterval )currentTimeStamp;

+ (NSTimeInterval )currentMeasureTimeStamp;

//过滤HTML标签
/**
 * 过滤标签
 */
+(NSString *)filterHTML:(NSString *)str;

+(NSString *)filterHTMLImage:(NSString *)str;
/**
 * 替换部分标签
 */
+ (NSString *)filterHTMLTag:(NSString *)str;

+ (NSString *)filterHTMLTags:(NSString *)htmlStr;

//获取当前系统语言
+ (NSString *)getCurrentLanguage;

/**
 *  随机生成UUID
 *
 *  @return
 */
+ (NSString *)uuidString;

/**
 参数排序

 @param obj 传入的字典
 @return 字符串
 */
+(NSString *)getNeedSignStrFrom:(NSDictionary *)dict;

/**
 ASE256加密
 
 @param content
 @param key
 @param iv
 @return
 */
+ (NSString *)encrypyAES:(NSString *)content key:(NSString *)key iv:(NSString *)iv;

/**
 ASE256解密
 
 @param content
 @param key
 @param iv
 @return
 */
+ (NSString *)descryptAES:(NSString *)content key:(NSString *)key iv:(NSString *)iv;

/**
 设置view指定位置的边框
 
 @param originalView   原view
 @param color          边框颜色
 @param borderWidth    边框宽度
 @param borderType     边框类型 例子: UIBorderSideTypeTop|UIBorderSideTypeBottom
 @return  view
 */
+ (UIView *)borderForView:(UIView *)originalView color:(UIColor *)color borderWidth:(CGFloat)borderWidth borderType:(UIBorderSideType)borderType;

/// 多个空格替换
/// @param text 替换空格文字
+(NSString *)replacingSpaceWithText:(NSString *)text;


+ (void)configWebViewJSMethod:(WKUserContentController *)contentVc delegate:(id <WKScriptMessageHandler>)vc;
+ (void)removeWebViewJSMethod:(WKUserContentController *)contentVc;

+ (NSString *)chindDateFormate:(NSDate *)update;

+ (NSString *)homePath;             // 程序主目录，可见子目录(3个):Documents、Library、tmp

+ (NSString *)appPath;              // 程序目录，不能存任何东西

+ (NSString *)docPath;              // 文档目录，需要ITUNES同步备份的数据存这里，可存放用户数据

+ (NSString *)libPrefPath;          // 配置目录，配置文件存这里

+ (NSString *)libCachePath;         // 缓存目录，系统永远不会删除这里的文件，ITUNES会删除

+ (NSString *)tmpPath;              // 临时缓存目录，APP退出后，系统可能会删除这里的内容

+ (NSString *)iapReceiptPath;       //用于存储iap内购返回的购买凭证

//view转成image
+ (UIImage*)imageWithUIView:(UIView*)view;
/// 获取跟控制器
+ (UIViewController *)cn_getRootViewController;
/// 获取当前控制器
+ (UIViewController *)cn_findVisibleViewController;


/**

 @param quality 视频质量的枚举
 @return 视频质量的描述
 */
+ (NSString *)stringWithQuality:(NSString *)quality;


/// 读取本地json文件
/// @param jsonname 文件名
+ (id)getJsonDataJsonname:(NSString *)jsonname;

/// 获取WKWebView加载时间
/// @param infoWebView webView
+(void)jsTimingWithWKWebView:(WKWebView *)infoWebView;


/// 根据url 获取所有utm_参数
/// @param url
+ (NSDictionary *)getUtmParamsWithURL:(NSURL *)url;
@end
